# -*- coding:utf-8 -*-

import os
from time import sleep
from selenium.webdriver.support.ui import Select
from extScript.UploadPic import UploadPic
from extScript.ClickTimeControl import ClickTimeControl


class LQ:
    def __init__(self):
        pass

    def lq(self, driver, dist=None):
        Select(driver.find_element_by_id("immovablesClass")).select_by_visible_text("林权")
        driver.find_element_by_id("immovablesDetail").clear()
        driver.find_element_by_id("immovablesDetail").send_keys(dist["不动产具体描述"])
        Select(driver.find_element_by_id("immovablesGetWay")).select_by_visible_text(dist["不动产取得方式"])

        driver.find_element_by_id("immovablesLoc").clear()
        driver.find_element_by_id("immovablesLoc").send_keys(dist["不动产坐落"])
        driver.find_element_by_id("immovablesArea").clear()
        driver.find_element_by_id("immovablesArea").send_keys(str(dist["面积"]))

        if dist["证件类型"] == "不动产证号":
            driver.find_element_by_id("immovablesNo").clear()
            driver.find_element_by_id("immovablesNo").send_keys(dist["不动产证号"])
        else:
            driver.find_element_by_id("forestNo").clear()
            driver.find_element_by_id("forestNo").send_keys(u"权证编号")

        driver.find_element_by_id("foresteastTo").clear()
        driver.find_element_by_id("foresteastTo").send_keys(str(dist["东"]))
        driver.find_element_by_id("forestwestTo").clear()
        driver.find_element_by_id("forestwestTo").send_keys(str(dist["西"]))
        driver.find_element_by_id("forestsouthTo").clear()
        driver.find_element_by_id("forestsouthTo").send_keys(str(dist["南"]))
        driver.find_element_by_id("forestnorthTo").clear()
        driver.find_element_by_id("forestnorthTo").send_keys(str(dist["北"]))
        driver.find_element_by_id("forestUseLim").clear()
        driver.find_element_by_id("forestUseLim").send_keys(str(dist["林地使用期限"]))

        driver.execute_script("arguments[0].scrollIntoView();",
                              driver.find_element_by_id("foresteastTo"))

        ctc = ClickTimeControl(driver, str(dist["林地使用终止日期"])[0:10])
        # 点击开始控件
        driver.find_element_by_id("forestEndDate").click()

        # 选择开始时间
        ctc.click_year("/html/body/div[2]/div[5]/table/tbody/tr/td/span")
        ctc.click_month("/html/body/div[2]/div[4]/table/tbody/tr/td/span")
        ctc.click_day("/html/body/div[2]/div[3]/table/tbody/tr")

        driver.find_element_by_xpath("/html/body/content/div[1]/form/div/div[11]/div[6]/div[1]").click()

        driver.find_element_by_name("forestRight.authorization").click()

        # 《林地使用合同》电子件或照片
        UploadPic(driver, "/html/body/content/div[1]/form/div/div[11]/div[7]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["《林地使用合同》电子件或照片"]).upfile(
            "/html/body/content/div[1]/form/div/div[11]/div[7]/div[3]/div[1]/div[1]/div[2]/a")

        driver.execute_script("arguments[0].scrollIntoView();",
                              driver.find_element_by_xpath("/html/body/content/div[1]/form/div/div[11]/div[13]/div[1]"))

        driver.find_element_by_id("smallPlaceName").clear()
        driver.find_element_by_id("smallPlaceName").send_keys(dist["小地名"])
        driver.find_element_by_id("compartment").clear()
        driver.find_element_by_id("compartment").send_keys(dist["林班"])
        driver.find_element_by_id("subcompartment").clear()
        driver.find_element_by_id("subcompartment").send_keys(dist["小班"])
        driver.find_element_by_id("mainBreed").clear()
        driver.find_element_by_id("mainBreed").send_keys(dist["主要树种"])

        driver.execute_script("arguments[0].scrollIntoView();",
                              driver.find_element_by_id("next-step2"))

        driver.find_element_by_id("plantsNum").clear()
        driver.find_element_by_id("plantsNum").send_keys(str(dist["林木株数"]))
        driver.find_element_by_name("interestedPartyView").click()

        UploadPic(driver, "/html/body/content/div[1]/form/div/div[18]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["利害关系人意见证明材料"]).upfile(
            "/html/body/content/div[1]/form/div/div[18]/div[3]/div[1]/div[1]/div[2]/a")

        driver.execute_script("arguments[0].scrollIntoView();",
                              driver.find_element_by_id("next-step2"))

        driver.find_element_by_id("textarea2").clear()
        driver.find_element_by_id("textarea2").send_keys(dist["备注"])

        driver.find_element_by_id("next-step2").click()
